<?php
    global $sid, $shopName,$productDAO, $shopDAO, $productcategoryDAO;	
     
    require_once ( LIB_DIR.'search/productsearch.php' );
     
     //商品分类
    $productCategory=$productDAO->GetProductCategoryBySid($sid);
     
     //热门商品
    $hotProductResult=$productDAO->GetHotProductBySid( $sid, 8 );
    
    $productCount=$productDAO->GetProductCountBySid($sid);
    //商品搜索列表

// if not search,    
if( !isset($_GET['q'] ) ){
    $productResult=array();
    $productResult=$productDAO->GetProductBySid( $sid );
    $productResult=$productResult['res'];
}
else{
    
    $searchType = FILTER_QUERY;

    $filterArray['shop_id'] = array(  $sid );
    if(isset($_GET['q'] ))	
    $keyword = htmlspecialchars( edp_db_input( $_GET['q'] ) );
    else 
    $keyword="";
    	
	$pcid='';
	if(!empty($_GET['pcid']))
	{
		$pcid=intval($_GET['pcid']); 
		$filterArray['cate_id'] = array( $pcid);
		
	}
	
	$orderasc='review_count-asc';
	$orderdesc='review_count-desc';
	$sortby = '';
	if( isset( $_GET['sortby'] ) )
	{	
		$sortby = $_GET['sortby'];
	}
		
	$isDesc = false;
	if( !empty( $sortby ) ) {
		
		$sortby1 = $sortby;
	
		$sortbyArray = explode( "-", $sortby );
		if( count($sortbyArray) == 2 ){
			$sortby = $sortbyArray[0];
			$isDesc = $sortbyArray[1] == "asc"? false : true;
			$orderasc = $sortbyArray[0].'-asc';
			$orderdesc = $sortbyArray[0].'-desc';
		}else {
			$sortby = "";
		}
		
		if( $sortby != "" ){
			$searchType = FILTER_SORT_QUERY;
		}	
	}
	
	$page='1';
	if(isset($_GET[UP_RESULT_PAGE]))
	{
		$page=intval($_GET[UP_RESULT_PAGE]);
	}
			
	
	$productSearch = new ProductSearch();
		
	$result = array();
	switch( $searchType ) {

		case FILTER_QUERY:
			$result = $productSearch->MultiFilterQuery( $keyword, $filterArray, $page );
			break;
		case FILTER_SORT_QUERY:
			$result = $productSearch->MultiFilterAndSortQuery( $keyword, $filterArray, $sortby, $page, $isDesc );
			break;
	}
	
	if( $result['total'] > 0 && !empty($result['matchIds']) ) {
		$dbRes = $productDAO->GetProductInfoByIdList2( $result['matchIds'] );
		
		$highLighRes = array();
		foreach( $dbRes as $row ) {
			$row['product_name_plain'] = $row['product_name'];
			//assemble field to highlight : shopname, shop description, nick, location, paymethod, tag, feature
			$arrToHighlight = array
				(
					//hard code here, need to be modified later
					'product_name' => $row['product_name'],
				);
			// merge database and high light result array
			$row = array_merge( $row, $productSearch->BuildHighLight( $keyword, $arrToHighlight ) );

			$highLighRes[] = $row;
		}

		$productResult = $highLighRes;
			
		
		/*
		$productResult=array();
	
		switch($sortby)
		{
			case '1':
			{
				$productResult=$productDAO->GetProductBySid($sid,$pcid,'price+0',PRODUCT_INFO_PER_PAGE,$page,$sort);
				break;
			}
			case '2':
			{
				$productResult=$productDAO->GetProductBySid($sid,$pcid,'create_time',PRODUCT_INFO_PER_PAGE,$page,$sort);
				break;
			}
			default:
			{
				$productResult=$productDAO->GetProductBySid($sid,$pcid,'review_count',PRODUCT_INFO_PER_PAGE,$page,$sort);
				break;
			}
		}*/
	}	
}
	
		/*
		 * Page bar
		 */
		require_once ( LIB_DIR.'classes/Pager.php' );
		
		if( isset( $sortby1 ) )
			$sortby = $sortby1;
		
		$currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
		$currentURL = preg_replace( '/&p=[0-9]+/', "", $currentURL );
		$pageObj = new Pager( $currentURL, $result['total'], PRODUCT_INFO_PER_PAGE, $page, UP_RESULT_PAGE );
?>
